package com.todod.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.todod.entity.ResultData;
import com.todod.mapper.DataDictoryCatalogMapper;
import com.todod.model.DataDictoryCatalog;
import com.todod.model.DataDictoryFile;
import com.todod.service.IDataDictoryCatalogService;
import com.todod.service.IDataDictoryFileService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author dyl
 * @date 2022-10-05
 */
@RestController
@RequestMapping("/dataDictoryCatalog")
public class DataDictoryCatalogController extends BaseManageController {

	@Autowired
	public IDataDictoryCatalogService iDataDictoryCatalogService;
	@Autowired
	public DataDictoryCatalogMapper iDataDictoryCatalogMapper;
	@Autowired
	private IDataDictoryFileService iDataDictoryFileService;

	/**
	 * 
	 * @param _userId
	 * @param request
	 * @param queryDataDictoryFile
	 * @return
	 */
	@RequestMapping("/getTreeList")
	public ResultData getTreeList(@RequestAttribute Long _userId, Long xiangmuId) {

		// 获取个人创建数据
		List<DataDictoryCatalog> list = iDataDictoryCatalogService.getListByXmId(xiangmuId);

		JSONObject jo = new JSONObject();
		JSONArray ja = JSONArray.fromObject(jo);
		ja.remove(0);

		jo.put("id", xiangmuId);
		jo.put("name", "数据库设计目录");
		jo.put("parentId", "-1");
		jo.put("xmId", xiangmuId);
		jo.put("sortNum", "0");
		jo.put("flag", "ML");
		ja.add(jo);

		if (list != null && list.size() > 0) {
			for (DataDictoryCatalog iDataDictoryCatalog1 : list) {
				jo.put("id", iDataDictoryCatalog1.getId());
				jo.put("name", iDataDictoryCatalog1.getName());
				jo.put("parentId", iDataDictoryCatalog1.getParentId());
				jo.put("xmId", iDataDictoryCatalog1.getXmId());
				jo.put("sortNum", iDataDictoryCatalog1.getSortNum());
				jo.put("flag", "ML");
				ja.add(jo);

				// 获取个人创建数据
				List<DataDictoryFile> plist = iDataDictoryFileService.getListByClassifyId(iDataDictoryCatalog1.getId());
				for (DataDictoryFile iDataDictoryFile : plist) {
					jo.put("id", iDataDictoryFile.getId());
					jo.put("name", iDataDictoryFile.getTableName());
					jo.put("parentId", iDataDictoryFile.getClassifyId());
					jo.put("xmId", iDataDictoryFile.getXmId());
					jo.put("sortNum", iDataDictoryFile.getSortNum());
					jo.put("flag", "WD");
					jo.put("code", iDataDictoryFile.getCode());
					ja.add(jo);
				}
			}
		}

		return ResultData.success("ok", ja);
	}

	/**
	 * 
	 * @param _userId
	 * @param reqDataDictoryCatalog
	 * @return
	 */
	@PostMapping("/save")
	public ResultData saveData(@RequestAttribute Long _userId, @RequestBody DataDictoryCatalog iDataDictoryCatalog) {

		iDataDictoryCatalogService.saveData(_userId, iDataDictoryCatalog);
		return ResultData.success();
	}

	/**
	 * 
	 * @param _userId
	 * @param reqDataDictoryCatalog
	 * @return
	 */
	@PostMapping("/edit")
	public ResultData editData(@RequestAttribute Long _userId, @RequestBody DataDictoryCatalog iDataDictoryCatalog) {

		iDataDictoryCatalogService.editData(_userId, iDataDictoryCatalog);
		return ResultData.success();
	}

	/**
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public ResultData delete(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}

		List<DataDictoryFile> list = iDataDictoryFileService.getListByXftId(code);

		if (list != null && list.size() > 0) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "当前目录下已有文档，请先删除文档再删除目录");
		}

		iDataDictoryCatalogMapper.deleteById(code);
		return ResultData.success();
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	@GetMapping("/getInfo")
	public ResultData getInfo(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}
		return ResultData.success("ok", iDataDictoryCatalogService.getInfo(code));
	}

	/**
	 * @Title: getLeafNode
	 * @Description: 获取全部叶子节点
	 * @author dyl
	 * @date: 2021-01-28
	 * @return
	 * @return Result
	 */
	@GetMapping("/getAllLeafNode")
	public ResultData getAllLeafNode(Long xiangmuId) {

		// 获取个人创建数据
		List<DataDictoryCatalog> list = new ArrayList<DataDictoryCatalog>();

		DataDictoryCatalog iDataDictoryCatalog = new DataDictoryCatalog();
		iDataDictoryCatalog.setId(xiangmuId);
		iDataDictoryCatalog.setParentId(-1L);
		iDataDictoryCatalog.setSortNum(0);
		iDataDictoryCatalog.setName("数据库设计目录");
		iDataDictoryCatalog.setXmId(xiangmuId);

		list.add(iDataDictoryCatalog);

		List<DataDictoryCatalog> list1 = iDataDictoryCatalogService.getListByXmId(xiangmuId);
		if (list1 != null && list1.size() > 0) {
			list.addAll(list1);
		}

		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getInitialLoading")
	public ResultData getInitialLoading(@RequestAttribute Long _userId, Long xmId) {
		// 获取权限数据
		List<DataDictoryCatalog> list = iDataDictoryCatalogService.getListByUserIdAndType(_userId, xmId);
		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getAgainLoading")
	public ResultData getAgainLoading(@RequestAttribute Long _userId, Long xmId, Long parentId) {
		// 获取权限数据
		List<DataDictoryCatalog> list = iDataDictoryCatalogService.getListByUserIdAndTypeAndParentId(_userId, xmId,
				parentId);
		return ResultData.success("ok", list);
	}

}
